<?php
declare(strict_types=1);

use Phinx\Migration\AbstractMigration;

class CreateTableUsers extends AbstractMigration
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change()
    {
        $table = $this->table('users', [
            'engine'    => 'innodb',
            'comment'   => '用户表',
        ]);

        $table->addColumn('username', 'string', [
            'null'      => false,
            'length'    => 32,
            'comment'   => '用户名',
        ]);
        $table->addColumn('password', 'string', [
            'null'      => false,
            'length'    => 255,
            'comment'   => '密码',
        ]);
        $table->addColumn('email', 'string', [
            'null'      => true,
            'length'    => 64,
            'comment'   => '邮箱',
        ]);
        $table->addColumn('salt', 'string', [
            'null'      => false,
            'length'    => 12,
            'comment'   => '密码盐',
        ]);
        $table->addColumn('created_at', 'datetime', [
            'comment'   => '创建时间',
            'default'   => 'CURRENT_TIMESTAMP',
        ]);
        $table->addColumn('updated_at', 'datetime', [
            'comment'   => '更新时间',
            'default'   => 'CURRENT_TIMESTAMP',
        ]);

        $table->addIndex(['username', 'email'], ['unique' => true]);

        $table->create();
    }
}
